﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beeflys.Generator
{
    public sealed class ProtocolGroupGenerator : IGenerator
    {
        public string Name { get; private set; }


        private List<ProtocolReqPackGenerator> protocolGenerators = new List<ProtocolReqPackGenerator>();
        internal ProtocolReqAdapterGenerator ReqAdapter { get; private set; }
        internal ProtocolResAdapterGenerator ResAdapter { get; private set; }

        public void Start(UTypeInfo protocolGroup)
        {
            this.Name = protocolGroup.Name + "_ProtocolGroupImpls";

            var methods = protocolGroup.Methods;
            foreach (var method in methods)
            {
                var protocolGenerator  = new ProtocolReqPackGenerator();
                protocolGenerator.Start(method);
                this.protocolGenerators.Add(protocolGenerator);
            }

            this.ReqAdapter = new ProtocolReqAdapterGenerator();
            this.ReqAdapter.Start(protocolGroup);
            this.ResAdapter = new ProtocolResAdapterGenerator();
            this.ResAdapter.Start(protocolGroup);

            //this.GenerateRequestWrap(protocolGroup);
            //this.GenerateResponseWrap(protocolGroup);
        }

        //
        private void GenerateRequestWrap(UTypeInfo protocolGroup)
        {
            
        }

        private void GenerateResponseWrap(UTypeInfo protocolGroup)
        {

        }

        public string Build()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using System.Threading.Tasks;");
            sb.AppendLine("using Beeflys;");

            sb.AppendLine($"namespace {Mapper.Namespace}.beeflys.actor.messenger.generated");
            sb.AppendLine("{");
            
            foreach (var protocolGenerator in protocolGenerators)
            {
                sb.AppendLine(protocolGenerator.Build());
            }

            sb.AppendLine(this.ReqAdapter.Build());
            sb.AppendLine(this.ResAdapter.Build());

            sb.AppendLine("}");




            return sb.ToString();
        }
    }
}
